package control.table
{
	import core.Model;
	import core.View;
	
	import events.TableEvent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	
	import global.GameState;
	import global.Resource;
	
	import model.result.ResultData;
	import model.result.RoundResultData;
	
	import utils.DisplayTool;
	import utils.animate.AlphaMoveTween;
	import utils.animate.TweenLight;

	public class TableFlyGoldControl
	{
		private var _view:View = View.getInstance();
		private var _model:Model = Model.getInstance();
		private var golds:Array;
		private var fireGold:DisplayObject;
		private var tweens:Array;
		
		public function TableFlyGoldControl()
		{
			initGolds();
			initTweens();
			initListener();	
		}
		
		private function initListener():void
		{
			_model.tableModel.addEventListener(TableEvent.ROUND_RESULT,onRoundResult);
		}
		
		
		
		private function initTweens():void
		{
			tweens = [];
			var tween:AlphaMoveTween;
			for (var i:int=0; i<6; i++)
			{
				tween = new AlphaMoveTween();
				tween.duration = 2;
				//tween.addEventListener(Event.COMPLETE,onTweenComplete);
				tweens.push(tween);
			}
		}
		
		private function onTweenComplete(event:Event):void
		{
			var tween:AlphaMoveTween = event.target as AlphaMoveTween;
			if (tween.target)
			{
				DisplayTool.removeChild(tween.target.parent,tween.target);
			}
		}
		
		private function initGolds():void
		{
			golds = [];
			var gold:DisplayObject;
			for (var i:int=0; i<6;i++)
			{
				gold = Resource.getImg("face_gold");
				golds.push(gold);
			}
			fireGold = Resource.getImg("face_golds");
		}
		
		private function onRoundResult(event:TableEvent):void
		{
			clear();
			var roundResultData:RoundResultData = event.roundResultData;
			
			if (roundResultData == null)
			{
				return;
			}
			
			if (roundResultData.winnerSitId == -1)
			{
				return;
			}
			
			if (roundResultData.fireSitId == -1)
			{
				normalFly(roundResultData);
			}
			else
			{
				fireFly(roundResultData);
			}
		}
		
		private function fireFly(roundResultData:RoundResultData):void
		{
			var winnerSitId:int = roundResultData.winnerSitId;
			var winner:Point = new Point(GameState.giveCardXYS[winnerSitId][0],GameState.giveCardXYS[winnerSitId][1]);
			var fireSitId:int = roundResultData.fireSitId;
			var fire:Point = new Point(GameState.giveCardXYS[fireSitId][0],GameState.giveCardXYS[fireSitId][1]);
			var tween:AlphaMoveTween = tweens[fireSitId];
			_view.tableLayer.addGold(fireGold);
			fireGold.alpha = 1;
			tween.setTarget(fireGold,fire.x,fire.y,winner.x,winner.y);
			tween.start();
		}
		
		private function normalFly(roundResultData:RoundResultData):void
		{
			var winnerSitId:int = roundResultData.winnerSitId;
			var winner:Point = new Point(GameState.giveCardXYS[winnerSitId][0],GameState.giveCardXYS[winnerSitId][1]);
			var sitId:int;
			var resultData:ResultData;
			var otherPoint:Point;
			var tween:AlphaMoveTween;
			var gold:DisplayObject;
			for (var i:int=0; i<roundResultData.resultDatas.length; i++)
			{
				resultData = roundResultData.resultDatas[i];
				sitId = resultData.sitId;
				
				if (sitId != winnerSitId)
				{
					
					otherPoint = new Point(GameState.giveCardXYS[sitId][0],GameState.giveCardXYS[sitId][1]);
					tween = tweens[sitId];
					gold = golds[sitId];
					gold.alpha = 1;
					//addChild(gold);
					_view.tableLayer.addGold(gold);
					tween.setTarget(gold,otherPoint.x,otherPoint.y,winner.x,winner.y - i * 5);
					trace(">>>>>:",gold.x,gold.y,otherPoint.x,otherPoint.y,winner.x,winner.y);
					tween.start();
				}
				
			}
		}
		
		public function clear():void
		{
			var tween:AlphaMoveTween;
			for (var i:String in tweens)
			{
				tween = tweens[i];
				tween.stop();
				DisplayTool.removeChild(golds[i].parent,golds[i]);
			}
			DisplayTool.removeChild(fireGold.parent,fireGold);
			
		}
	}
}